/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.report.db;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import blueprint4j.db.DBConnection;
import blueprint4j.db.DataException;
import blueprint4j.db.Entity;
import blueprint4j.db.FieldBoolean;
import blueprint4j.db.FieldDateTime;
import blueprint4j.db.FieldString;
import blueprint4j.db.FieldUnique;
import blueprint4j.db.Maintenance;
import blueprint4j.db.TableName;
import blueprint4j.utils.BindException;


/**
 * This class is an entity that represets a scheduled report that gets emailed
 */

public class ScheduleReportLog extends Entity {
    static String VERSION = "$Id: ScheduleReportLog.java,v 1.2 2004/08/05 11:38:10 jaspervdb Exp $";
    
    static final String TABLE_NAME = "d_sch_rpt_logs";
    
    public static ScheduleReport CURRENT_USER = null;
    
    private static List<String> PRIVILEGES = null;
    
    public TableName table_name = new TableName(TABLE_NAME, this);
    
    public FieldUnique id = new FieldUnique("id", 0, "The Record Identifier", this);
    public FieldString schedule_report_id = new FieldString("schedule_report_id",0,"schedule_report_id",26,this);
    public FieldBoolean success = new FieldBoolean("success",0,"Sucessfull execution of report",this);
    public FieldString message = new FieldString("message",0,"message",250,this);
    public FieldDateTime last_execution = new FieldDateTime("last_execution",0,"last_execution",this);
    
    private Maintenance maintenance = new Maintenance(this,new Maintenance.Action[]{new Maintenance.ActionTrim(500)},60*1000*60*5);
    
    
    public ScheduleReportLog() {
        super();
    }
    
    public ScheduleReportLog(DBConnection connection)
    throws DataException, SQLException {
        super(connection);
    }
    
    public Entity getNewInstance() {
        return new ScheduleReportLog();
    }
    
    public String getVersionString() {
        return VERSION;
    }
    
    public static void newLog(DBConnection connection,boolean p_success,String p_message,ScheduleReport schedule_report)
    throws SQLException,BindException {
        ScheduleReportLog srl = new ScheduleReportLog(connection);
        srl.schedule_report_id.set(schedule_report.id.get());
        srl.last_execution.set(new Date());
        srl.success.set(new Boolean(p_success));
        srl.message.set(p_message);
        srl.save();
        connection.commit();
    }
    
    public static ScheduleReportLog getLastExecute(DBConnection connection,ScheduleReport schedule_report)
    throws SQLException {
        return (ScheduleReportLog) new ScheduleReportLog(connection).find(
                Entity.limitSQL(
                connection,
                "schedule_report_id = " +
                Entity.asSQL(schedule_report.id.get()) +
                "order by last_execution desc"
                ,1)
                );
    }
}
